package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UploadDialog extends DialogBox {

    // You must use a FormPanel to create a blobstore upload form
    final FormPanel uploadForm = new FormPanel();

    // Use an RPC call to the Blob Service to get the blobstore upload url

    VerticalPanel mainVerticalPanel = new VerticalPanel();
    HorizontalPanel hp1 = new HorizontalPanel();
    HorizontalPanel hp2 = new HorizontalPanel();
    HTML titleLabel = new HTML("Title");
    HTML descriptionLabel = new HTML("Description");
    TextBox titleTextBox = new TextBox();
    TextBox descriptionTextBox = new TextBox();
    FileUpload upload = new FileUpload();
    Button submitButton = new Button("Submit");

    FlexTable resultsTable = new FlexTable();

    public UploadDialog() {
	super(true);
	this.hp1.add(this.titleLabel);
	this.hp1.add(this.titleTextBox);
	this.hp2.add(this.descriptionLabel);
	this.hp2.add(this.descriptionTextBox);

	this.mainVerticalPanel.add(this.hp1);
	this.mainVerticalPanel.add(this.hp2);
	this.mainVerticalPanel.add(this.upload);

	this.mainVerticalPanel.add(this.submitButton);
	this.mainVerticalPanel.add(this.resultsTable);

	this.hp1.setSpacing(5);
	this.hp2.setSpacing(5);
	this.mainVerticalPanel.setSpacing(5);

	this.uploadForm.setWidget(this.mainVerticalPanel);

	// The upload form, when submitted, will trigger an HTTP call to the
	// servlet.  The following parameters must be set
	this.uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
	this.uploadForm.setMethod(FormPanel.METHOD_POST);

	// Set Names for the text boxes so that they can be retrieved from the
	// HTTP call as parameters
	this.titleTextBox.setName("titleTextBox");
	this.descriptionTextBox.setName("descriptionTextBox");
	this.upload.setName("upload");

	RootPanel.get("container").add(this.uploadForm);

	this.submitButton.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {

		Feeds.getService().getBlobStoreUploadUrl(new AsyncCallback<String>() {

		    @Override
		    public void onFailure(Throwable caught) {
			caught.printStackTrace();
		    }

		    @Override
		    public void onSuccess(String result) {
			// Set the form action to the newly created
			// blobstore upload URL
			UploadDialog.this.uploadForm.setAction(result.toString());

			// Submit the form to complete the upload
			UploadDialog.this.uploadForm.submit();
			UploadDialog.this.uploadForm.reset();
		    }
		});

	    }
	});

	this.uploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
	    @Override
	    public void onSubmitComplete(SubmitCompleteEvent event) {

	    }

	});
	setWidget(this.uploadForm);

    }
}
